package com.yidu.ladder.base.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yidu.ladder.base.domain.Area;
import com.yidu.ladder.base.domain.AreaVo;
import com.yidu.ladder.base.service.AreaService;
import com.yidu.ladder.util.POIUtil;
/**
 * 区域控制器
 * @author lw
 * @since 2020/1/10 
 */
@Controller
@RequestMapping("/base")
public class AreaController {
	
	/**
	 * 将区域业务层依赖注入
	 */
	@Autowired
	private AreaService areaService;
	
	
	public AreaController() {
	}

	public AreaController(AreaService areaService) {
		super();
		this.areaService = areaService;
	}

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	/**
	 * 文件上传
	 * @param myFile
	 * @return
	 * @throws IOException
	 */
	 @RequestMapping(value = "/upload",method = RequestMethod.POST)
	 @ResponseBody
	 private String uploadExcel(MultipartFile myFile) throws IOException {
	     try{
	        //这里得到的是一个集合，里面的每一个元素是String[]数组
	        List<String[]> list = POIUtil.readExcel(myFile); 
	        //调用业务逻辑层中service实现方法 
	        areaService.saveBath(list); 
	     } catch(IOException e){
	    	//给变量赋值为1
	    	 return "1";
	     }
	     //返回结果
	     return "0";
	 } 
	 
	 /**
	  * 组合条件查询所有带分页
	  * @param areaVo
	  * @return
	  */
	 @RequestMapping(value="/areas",method=RequestMethod.GET)
	 @ResponseBody
	 public Map<String, Object> findAllArea(AreaVo areaVo){
		Map<String, Object> result = areaService.findAllAreaWithPage(areaVo);
		return result;
	 }
	 
	 /**
	  * 修改区域
	  * @param area
	  * @return 
	  */
	 @RequestMapping(value="/area",method=RequestMethod.PUT)
	 @ResponseBody
	 public Object updataArea(Area area) {
		return areaService.updataArea(area);
	 }
	 
	 /**
	  * 逻辑删除
	  * @param areaIdStr 
	  * @return
	  */
	 @PostMapping("/frozenORrecoverArea")
	 @ResponseBody
	 public boolean frozenORrecoverArea(String areaIdStr) {
		boolean result = areaService.frozenORrecoverArea(areaIdStr,true);
		return result;
	 }
	 
	 /**
	  * 还原
	  * @param areaIdStr
	  * @return
	  */
	 @PostMapping("/restoreRrecoverArea")
	 @ResponseBody
	 public boolean restoreRrecoverArea(String areaIdStr) {
		boolean result = areaService.frozenORrecoverArea(areaIdStr,false);
		return result;
	 }
	 
	 /**
	  * 文件上传
	  * @param request
	  * @return
	  * @throws Exception
	  */
     @RequestMapping("/import")
     @ResponseBody
     public String impotr(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
        MultipartFile file = multipart.getFile("file");
        String filename = request.getParameter("filename");
        InputStream in = file.getInputStream();
        System.out.println(filename);
         //数据导入
        areaService.insertdb(in,file);
        in.close();
        return "true";
     }
     
     /**
      * 查询所有
      * @return
      */
     @GetMapping("/findAll")
     @ResponseBody
     public List<Area> findAllArea(){
    	List<Area> result = areaService.findAll();
		return result;
     }
     
     /**
      * 查询所有的省
      * @return
      */
     @GetMapping("/findAllProvince")
     @ResponseBody
     public List<Area> findAllProvince(){
    	List<Area> result = areaService.queryAllProvince();
		return result;
     }
     
     /**
      * 查询某个省下的所有市
      * @param province
      * @return
      */
     @GetMapping("/findAllCity")
     @ResponseBody
     public List<Area> findAllCity(String province){
    	List<Area> result = areaService.queryAllCity(province);
		return result;
     }
     
     /**
      * 查询某个市下所有的县(区)
      * @param city
      * @return
      */
     @GetMapping("/findAllDistrict")
     @ResponseBody
     public List<Area> findAllDistrict(String city){
    	List<Area> result = areaService.queryAllDistrict(city);
		return result;
     }

}
